﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace task2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            double number1, number2, result = 0;
            string operation = comboBox1.SelectedItem?.ToString();

            // 尝试将文本框输入转换为数字
            if (double.TryParse(textBox1.Text, out number1) && double.TryParse(textBox2.Text, out number2))
            {
                // 根据选择的运算符进行计算
                switch (operation)
                {
                    case "+":
                        result = number1 + number2;
                        break;
                    case "-":
                        result = number1 - number2;
                        break;
                    case "*":
                        result = number1 * number2;
                        break;
                    case "/":
                        if (number2 != 0)
                        {
                            result = number1 / number2;
                        }
                        else
                        {
                            MessageBox.Show("除数不能为零。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        break;
                    case "%":
                        result = number1 % number2;
                        break;
                    default:
                        MessageBox.Show("请选择一个有效的运算符。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                }
                // 显示结果
                labelResult.Text = "结果: " + result.ToString();
                labelResult.Visible = true; 

            }
            else
            {
                MessageBox.Show("请输入有效的数字。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
